<template>
  <div class="sdnew">
    <van-overlay :show="overlayShow" :z-index="1000">
      <div style="display: flex;align-items: center;justify-content: center;height: 100%;">
        <van-loading type="spinner" color="#1989fa">请稍候...</van-loading>
      </div>
    </van-overlay>
    <div style="margin-bottom: 10vh;">
      <van-collapse v-model="activeNames">
        <van-collapse-item size="large" v-for="item in jsondata.head" :key="item.grouptitle" :name="item.grouptitle" :title="item.grouptitle">
          <van-cell-group style="border:0px;">
            <div style="border-bottom:1px solid #ebedf0" v-for="itemdata in item.groupvalue" :key="itemdata.ID">
              <idatetime ishead :itemdata="itemdata" v-if="itemdata.sUIType == 'datetime'" :bzjsrqField.sync="bzjsrqField" :xzksrqField.sync="xzksrqField" :xzjsrqField.sync="xzjsrqField" />
              <icode ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'code'" />
              <ienum @handleFiledLink="handleFiledLink" ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'enum'" :jsondata="jsondata" :idobject="idobject" />
              <iref @handleFiledLink="handleFiledLink" ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'ref'" :clickmodel.sync="clickmodel" :clickfrom.sync="clickfrom" :clickindexName.sync="clickindexName" :clickindex.sync="clickindex" :jsondata="jsondata" :activeNames="activeNames" />
              <ibool @handleFiledLink="handleFiledLink" ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'bool'" :jsondata="jsondata" :idobject="idobject" />
              <ifile ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'file'" :jsondata="jsondata" />
              <ipicture ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'picture'" :jsondata="jsondata" />
              <istring @handleFiledLink="handleFiledLink" ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'string'" :jsondata="jsondata" :idobject="idobject" />
              <imap ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'map'" :jsondata="jsondata" :item="item" />
              <istring @handleFiledLink="handleFiledLink" ishead :itemdata="itemdata" v-else :jsondata="jsondata" :idobject="idobject" />

            </div>
          </van-cell-group>
        </van-collapse-item>
      </van-collapse>
      
      
      
     <!-- 子表 -->
     <body-data :jsondata="jsondata" :detailTitle="detailTitle" :activeBodyNams="activeBodyNams"
        :clickmodel.sync="clickmodel" :clickfrom.sync="clickfrom" :handleFiledLink="handleFiledLink"
        :clickindexName.sync="clickindexName" :clickindex.sync="clickindex" :addRow="addRow" :delRow="delRow"></body-data>
    </div>

    
    <van-popup v-model="datepickershow" round position="bottom">
      <van-datetime-picker v-model="currentDate" type="date" @confirm="selectedDate" />
    </van-popup>
    <van-popup v-model="yearmonthshow" round position="bottom">
      <van-datetime-picker v-model="currentYearMonth" type="year-month" @confirm="selectedYearMonth" />
    </van-popup>
    <van-action-sheet v-model="enumshow" :actions="actions" @select="selectedEnum" />
    <van-popup v-model="enumMultishow" round position="bottom">
      <van-checkbox-group v-model="selecteddata" ref="checkboxGroup">
        <van-cell v-for="(item, index) in enumMulti" :key="item.id" :title="item.name" @click="toggle(index)">
          <van-checkbox :name="item" ref="checkboxes" slot="right-icon" />
        </van-cell>
      </van-checkbox-group>
      <div style="text-align:right;padding-right:10vw;padding-top:2vh;padding-bottom:2vh;">
        <van-button type="info" size="small" @click="selectedEnumMulti">确认</van-button>
      </div>
    </van-popup>
    <van-tabbar v-model="active" safe-area-inset-bottom>
      <van-tabbar-item v-if="this.beforeSave && !this.afterSaveSucceed" @click="backsd">{{ sdtitle }}</van-tabbar-item>
      <van-tabbar-item v-if="this.beforeSave && !this.afterSaveSucceed" @click="save">保存</van-tabbar-item>
      <van-tabbar-item v-if="isflow && this.beforeSave && !this.afterSaveSucceed && 'hdzjd' !== this.idobject" @click="savesubmit">保存并提交</van-tabbar-item>
      <van-tabbar-item v-if="isflow && !this.beforeSave && this.afterSaveSucceed" @click="submit">提交</van-tabbar-item>
      <van-tabbar-item v-if="!isflow && this.beforeSave && !this.afterSaveSucceed && this.idobject == 'arvouch'" @click="saveAudit">保存并审核</van-tabbar-item>
      <!-- <van-tabbar-item v-if="this.beforeSave && !this.afterSaveSucceed && this.idobject == 'arvouch'" @click="saveAudit">保存并审核</van-tabbar-item> -->
      <van-tabbar-item v-if="isflow && !this.beforeSave && this.afterSaveSucceed && this.idobject == 'arvouch'" @click="audit">审核</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
import Vue from 'vue'
import {
  Search,
  List,
  Cell,
  PullRefresh,
  Toast,
  Tabbar,
  TabbarItem,
  Popup,
  DatetimePicker,
  ActionSheet,
  Tab,
  Tabs,
  Collapse,
  CollapseItem,
  CheckboxGroup,
  Checkbox,
  Uploader,
  Overlay,
  Loading,
  NumberKeyboard,
  Dialog
} from 'vant'
import qs from 'qs'
import { keepNDecimalFull, base64encode } from '../assets/js/tool.js'
import useInput from '@/utils/useinput'
import { fieldLinkMixins } from '@/mixins/fieldLink.js'
import { fieldMapMixins } from '@/mixins/fieldmap.js'

import bodyData from '@/components/bodyData/bodyData'

Vue.use(Search).use(List).use(Cell).use(PullRefresh).use(Toast).use(Tabbar).use(TabbarItem).use(Popup).use(DatetimePicker).use(ActionSheet).use(Tab).use(Tabs).use(Collapse).use(CollapseItem).use(CheckboxGroup).use(Checkbox).use(Uploader).use(Overlay).use(Loading).use(NumberKeyboard).use(useInput)
export default {
  components: {
    "body-data": bodyData
  },
  name: 'sdnew',
  mixins: [fieldLinkMixins, fieldMapMixins],
  data () {
    return {
      sdtitle: '重选生单',
      idobject: this.$route.query.idobject,
      refobject: this.$route.query.refobject,
      relopeid: this.$route.query.relopeid,
      headid: this.$route.query.headid,
      bodyid: this.$route.query.bodyid,
      js: this.$route.query.js,
      sl: this.$route.query.sl,
      ksxd: this.$route.query.ksxd,
      jsondata: {
        bodydata: {},
        body: [],
        bodymetadata: {},
        headmetadata: {}
      },
      index: 0,
      active: 0,
      activeNames: [''],
      activeBodyNams: [],
      datepickershow: false,
      currentDate: new Date(),
      yearmonthshow: false,
      currentYearMonth: new Date(),
      enumshow: false,
      actions: [],
      clickmodel: {},
      isLoad: false,
      conditionValHidden: '',
      clickfrom: '',
      clickindex: 0,
      clickindexName: '',
      selecteddata: [],
      enumMultishow: false,
      enumMulti: [],
      isDisable: false,
      overlayShow: false,
      isflow: false,
      account3id: this.$route.query.account3id,
      // 以下两个状态判断是否保存, 来决定显示保存按钮 后者是 保存并审核 保存并提交 按钮
      // 未保存
      beforeSave: true,
      // 保存成功
      afterSaveSucceed: false,
      show: false,
      value: '',
      bzjsrqField: '',
      xzksrqField: '',
      xzjsrqField: ''
    }
  },
  created: function () {
    //console.log('start created')
    if (!this.isLoad) {
      let data = {
        relopeid: this.relopeid,
        headid: this.headid,
        bodyid: this.bodyid,
        idobject: this.idobject,
        refobject: this.refobject,
        ksxd: this.ksxd,
        js: this.js,
        sl: this.sl,
        account3id: this.account3id
      }
      this.axios({
        method: 'post',
        url: '/api/' + this.refobject + '.sdnew.do',
        data: qs.stringify(data)
      }).then(res => {
        //console.log(res)
        if (res.data.flag === '1') {
          Toast(res.data.des)
          if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
            this.loginout()
          } else {
            this.$router.go(-1)
          }
        } else {
          this.jsondata = res.data.jsondata

          // 字段联动变量
          this.orginData = JSON.parse(JSON.stringify(this.jsondata))
          this.controlLinkArr = this.getFieldLink(this.jsondata)

          //console.log('*****', this.controlLinkArr)

          this.isflow = res.data.isflow
          for (let i = 0; i < res.data.jsondata.head.length; i++) {
            this.activeNames.push(res.data.jsondata.head[i].grouptitle)
          }

          if (res.data.jsondata.body && Object.keys(res.data.jsondata.body).length > 0) {

            let k = 0
            for (const [key, value] of Object.entries(res.data.jsondata.body)) {
              this.activeBodyNams.push(`${key}${k}`)
              k++
            }
          }
          //console.log(this.jsondata)
          this.isLoad = true
        }
      }).catch(res => {
        this.reserror(res)
      })
    }
  },
  watch: {
    'itemdata.value': {
      handler (newval) {
        this.$emit('handleFiledLink', { sField: this.itemdata.sField, value: newval })
      }
    }
  },
  methods: {
    detailTitle (indexName, item, index) {
      for (let i = 0; i < item.length; i++) {
        if (item[i].sField == this.jsondata.titleField) {
          return (index + 1) + '.' + (item[i].value.length > 0 ? item[i].value : '新' + indexName)
        }
      }
      return indexName + (index + 1)
    },
    backsd () {
      /* this.$router.push({ name: 'sdlist',
        query: {
          idobject: this.idobject,
          sdtype: this.$route.query.sdtype,
          viewid: this.$route.query.viewid,
          subviewid: this.$route.query.subviewid,
          relopeid: this.$route.query.relopeid,
          refobject: this.$route.query.refobject,
          subrefobject: this.$route.query.subrefobject
      } }) */
      this.$router.go(-1)
    },
    save () {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                alert(itemdata.sTitle + '不能为空')
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        if (this.jsondata.hasdetail === '1') {
          for (let indexName in this.jsondata.bodydata) {
            for (let i = 0; i < this.jsondata.bodydata[indexName].length; i++) {
              let item = this.jsondata.bodydata[indexName][i]
              for (let itemindex in item) {
                let itemdata = item[itemindex]
                if (itemdata.sUIType === 'ref') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
                } else if (itemdata.sUIType === 'enum') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
                } else {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
                }
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          Toast('请检查必输项!')
          return
        }
        Object.assign(data, {
          sourceobjid: this.headid,
          sourceobjdetailid: this.bodyid,
          sourcebo: this.refobject,
        });
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.newsave.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            if (res.data.des !== '') {
              Toast(res.data.des + ' ')
            } else {
              Toast('保存成功')
            }
            this.removeMyKeyFromLocalStorage(this.account3id)
            this.$router.push({ path: 'card', query: { idobject: this.idobject,itemid:this.jsondata[`${this.idobject}_id`]}})
            // this.$router.push({ name: 'list', query: { idobject: this.idobject } })
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }

          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log(res)
        })
      }
    },
    submit () {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        data[this.idobject + '_idtemplate'] = this.idtemplate
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                alert(itemdata.sTitle + '不能为空')
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        if (this.jsondata.hasdetail === '1') {
          for (let indexName in this.jsondata.bodydata) {
            for (let i = 0; i < this.jsondata.bodydata[indexName].length; i++) {
              let item = this.jsondata.bodydata[indexName][i]
              for (let itemindex in item) {
                let itemdata = item[itemindex]
                if (itemdata.sUIType === 'ref') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
                } else if (itemdata.sUIType === 'enum') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
                } else {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
                }
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.flowsubmit.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            this.beforeSave = false
            this.afterSaveSucceed = true
            Toast('提交成功')
            this.removeMyKeyFromLocalStorage(this.account3id)
            this.$router.push({ name: 'list', query: { idobject: this.idobject } })
            // 发起提交请求结束 --
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log(res)
        })
      }
    },
    savesubmit () {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                alert(itemdata.sTitle + '不能为空')
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        if (this.jsondata.hasdetail === '1') {
          for (let indexName in this.jsondata.bodydata) {
            for (let i = 0; i < this.jsondata.bodydata[indexName].length; i++) {
              // for (let i = 0; i < this.jsondata.bodydata.length; i++) {
              let item = this.jsondata.bodydata[indexName][i]
              //console.log('item', item)
              for (let itemindex in item) {
                let itemdata = item[itemindex]
                if (itemdata.sUIType === 'ref') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
                } else if (itemdata.sUIType === 'enum') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
                } else {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
                }
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false

          this.overlayShow = false
          Toast('请检查必输项!')
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.newsave.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            this.isDisable = true
            this.overlayShow = true
            this.beforeSave = false
            this.afterSaveSucceed = true
            Toast('保存成功...开始提交...')
            // 发起提交请求开始 --
            this.axios({
              method: 'post',
              url: '/api/' + this.idobject + '.flowsubmit.do',
              data: qs.stringify(data)
            }).then(res => {
              this.isDisable = false
              this.overlayShow = false
              if (res.data.flag === '0') {
                Toast('提交成功')
                this.removeMyKeyFromLocalStorage(this.account3id)
                this.$router.push({ name: 'list', query: { idobject: this.idobject } })
              } else {
                Toast(res.data.des)
                if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
                  this.loginout()
                }
              }
              //console.log(res)
            }).catch(res => {
              this.isDisable = false
              this.overlayShow = false
              //console.log(res)
            })

            // 发起提交请求结束 --
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log(res)
        })
      }
    },
    saveAudit () {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        if (this.jsondata.hasdetail === '1') {
          for (let i = 0; i < this.jsondata.bodydata.length; i++) {
            let item = this.jsondata.bodydata[i]
            for (let itemindex in item) {
              let itemdata = item[itemindex]
              if (itemdata.sUIType === 'ref') {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
              } else if (itemdata.sUIType === 'enum') {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
              } else {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          return
        }

        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.newsave.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            this.isDisable = true
            this.overlayShow = true
            Toast('保存成功...开始审核...')
            this.removeMyKeyFromLocalStorage(this.account3id)
            this.beforeSave = false
            this.afterSaveSucceed = true
            // 发起审核请求开始 --
            this.axios({
              method: 'post',
              url: '/api/' + this.idobject + '.audit.do',
              data: qs.stringify(data)
            }).then(res => {
              this.isDisable = false
              this.overlayShow = false
              if (res.data.flag === '0') {
                Toast('审核成功')
                this.$router.push({ name: 'list', query: { idobject: this.idobject } })
              } else {
                Toast(res.data.des)
                if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
                  this.loginout()
                }
              }
              //console.log(res)
            }).catch(res => {
              this.isDisable = false
              this.overlayShow = false
              //console.log(res)
            })
            // 发起审核请求结束 --
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log(res)
        })
      }
    },
    audit () {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        if (this.jsondata.hasdetail === '1') {
          for (let i = 0; i < this.jsondata.bodydata.length; i++) {
            let item = this.jsondata.bodydata[i]
            for (let itemindex in item) {
              let itemdata = item[itemindex]
              if (itemdata.sUIType === 'ref') {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
              } else if (itemdata.sUIType === 'enum') {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
              } else {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.audit.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            Toast('审核成功')
            this.$router.push({ name: 'list', query: { idobject: this.idobject } })
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }

          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log(res)
        })
      }
    },
    addRow (indexName) {
      let bodycopy = JSON.parse(JSON.stringify(this.jsondata.body[indexName]))
      this.jsondata.bodydata[indexName].push(bodycopy)
    },
    delRow (indexName, index) {
      if (this.jsondata.bodydata[indexName].length === 1) {
        Toast('至少保留一条表体行，不能删除！')
        return
      }

      Dialog.confirm({
        title: '',
        message: '你确定要删除' + indexName + ' ' + (index + 1) + '吗?'
      })
        .then(() => {
          // on confirm
          this.jsondata.bodydata[indexName].splice(index, 1)
        })
        .catch(() => {
          // on cancel
        })
    },
    pageCal (indexName, sField, index) {
      if (indexName !== '销售订单明细' && indexName !== '需货计划明细' && indexName !== '销售计划（年）明细') {
        return
      }
      let item = this.jsondata.bodydata[indexName][index]
      let quantity = 0.0
      let xyj = 0.0
      let price = 0.0
      let money = 0.0
      let zr = 0.0
      let kl = 0.0
      let js = 0.0
      let bz = 0.0
      let djmoney = 0.0
      let dj = 0.0

      let quantityindex = ''
      let jsindex = ''
      let xyjindex = ''
      let priceindex = ''
      let moneyindex = ''
      // let djindex = ''
      let djmoneyindex = ''
      let zrindex = ''
      let klindex = ''
      for (let itemindex in item) {
        let itemdata = item[itemindex]
        //console.log(`itemdate: ${itemdata}`)
        if (itemdata.sField === 'quantity') {
          quantity = itemdata.value
          quantityindex = itemindex
        } else if (itemdata.sField === 'xyj') {
          xyj = itemdata.value
          xyjindex = itemindex
        } else if (itemdata.sField === 'price') {
          price = itemdata.value
          priceindex = itemindex
        } else if (itemdata.sField === 'money') {
          money = itemdata.value
          moneyindex = itemindex
        } else if (itemdata.sField === 'zr') {
          zr = itemdata.value
          zrindex = itemindex
        } else if (itemdata.sField === 'kl') {
          kl = itemdata.value
          klindex = itemindex
        } else if (itemdata.sField === 'js') {
          js = itemdata.value
          jsindex = itemindex
        } else if (itemdata.sField === 'bz') {
          bz = itemdata.value
        } else if (itemdata.sField === 'dj') {
          dj = itemdata.value
          // djindex = itemindex
        } else if (itemdata.sField === 'djmoney') {
          djmoney = itemdata.value
          djmoneyindex = itemindex
        }
      }
      if (!Number(xyj)) {
        xyj = price
      }
      if (sField === 'quantity') {
        money = quantity * price
        djmoney = quantity * dj
        zr = quantity * (xyj - (price))
        js = quantity / bz
      } else if (sField === 'xyj') {
        price = xyj * kl / 100
        money = quantity * price
        zr = quantity * (xyj - (price))
      } else if (sField === 'price') {
        money = quantity * price
        if (!Number(xyj)) {
          return
        }
        kl = price / xyj * 100
        zr = quantity * (xyj - (price))
      } else if (sField === 'money') {
        if (!Number(quantity)) {
          return
        }
        price = money / quantity
        if (!Number(xyj)) {
          return
        }
        kl = price / xyj * 100
        zr = quantity * xyj - (money)
      } else if (sField === 'zr') {
        money = quantity * xyj - (zr)
        if (!Number(quantity)) {
          return
        }
        price = money / quantity
        if (!Number(xyj)) {
          return
        }
        kl = price / xyj * 100
      } else if (sField === 'kl') {
        if (!Number(kl)) {
          return
        }
        price = xyj * kl / 100
        money = quantity * price
        zr = quantity * (xyj - (price))
      } else if (sField === 'js') {
        if (!Number(js)) {
          return
        }
        //console.log(js * bz)
        quantity = js * bz
        money = quantity * price
        djmoney = quantity * dj
      }
      if (!quantity) {
        quantity = 0.0
      }
      if (!js) {
        js = 0.0
      }
      if (!xyj) {
        xyj = 0.0
      }
      if (!price) {
        price = 0.0
      }
      if (!money) {
        money = 0.0
      }
      if (!dj) {
        dj = 0.0
      }
      if (!djmoney) {
        djmoney = 0.0
      }
      if (!zr) {
        zr = 0.0
      }
      if (!kl) {
        kl = 0.0
      }
      if (quantityindex) {
        item[quantityindex].value = keepNDecimalFull(quantity, 2)
      }
      if (jsindex) {
        item[jsindex].value = keepNDecimalFull(js, 2)
      }
      if (xyjindex) {
        item[xyjindex].value = keepNDecimalFull(xyj, 6)
      }
      if (priceindex) {
        item[priceindex].value = keepNDecimalFull(price, 6)
      }
      if (moneyindex) {
        item[moneyindex].value = keepNDecimalFull(money, 2)
      }
      if (djmoneyindex) {
        item[djmoneyindex].value = keepNDecimalFull(djmoney, 2)
      }
      if (zrindex) {
        item[zrindex].value = keepNDecimalFull(zr, 2)
      }
      if (klindex) {
        item[klindex].value = keepNDecimalFull(kl, 2)
      }
    },
    checkData (datamodel) {
      if (datamodel.value === '') {
        datamodel.errormsg = '不能为空'
      } else {
        datamodel.errormsg = ''
      }
    },
    openDatePicker (datamodel) {
      if (datamodel.value != null && datamodel.value !== undefined && datamodel.value !== '') {
        datamodel.value = datamodel.value.substring(0, 10)
        let newDate = new Date(datamodel.value.replace(/-/g, '/'))
        this.currentDate = new Date(newDate)
      }
      this.clickmodel = datamodel
      this.datepickershow = true
    },
    selectedDate () {
      //console.log(this.currentDate)
      let year = this.currentDate.getFullYear() // 获取完整的年份(4位,1970-????)
      let month = this.currentDate.getMonth() + 1 // 获取当前月份(0-11,0代表1月)
      let day = this.currentDate.getDate() // 获取当前日(1-31)
      if (month < 10) {
        month = '0' + month
      }
      if (day < 10) {
        day = '0' + day
      }
      let dateString = year + '-' + month + '-' + day
      this.clickmodel.value = dateString
      this.clickmodel.errormsg = ''
      this.datepickershow = false
    },
    openYearMonth (datamodel) {
      if (datamodel.value != null && datamodel.value !== undefined && datamodel.value !== '') {
        datamodel.value = datamodel.value.substring(0, 7)
        this.currentYearMonth = new Date(datamodel.value)
      }
      this.clickmodel = datamodel
      this.yearmonthshow = true
    },
    selectedYearMonth () {
      //console.log(this.currentYearMonth)
      let year = this.currentYearMonth.getFullYear()
      let month = this.currentYearMonth.getMonth() + 1
      if (month < 10) {
        month = '0' + month
      }
      let datestring = year + '-' + month
      this.clickmodel.value = datestring
      this.clickmodel.errormsg = ''
      this.yearmonthshow = false
    },
    openEnum (datamodel) {
      this.actions = []
      this.clickmodel = datamodel
      let enumid = datamodel.allenumid
      let arrEnumid = enumid.split(';')
      let enumval = datamodel.allenumval
      let arrEnumval = enumval.split(';')
      for (let i = 0; i < arrEnumid.length; i++) {
        this.actions.push({ id: arrEnumid[i], name: arrEnumval[i] })
      }
      this.enumshow = true
    },
    selectedEnum (item) {
      this.clickmodel.valuehidden = item.id
      this.clickmodel.value = item.name
      this.clickmodel.errormsg = ''
      this.enumshow = false
      this.boolUpdate(this.clickmodel)
    },
    openEnumMulti (datamodel) {
      this.enumMulti = []
      this.selecteddata = []
      this.clickmodel = datamodel
      let enumid = datamodel.allenumid
      let arrEnumid = enumid.split(';')
      let enumval = datamodel.allenumval
      let arrEnumval = enumval.split(';')
      for (let i = 0; i < arrEnumid.length; i++) {
        if (arrEnumid[i] === '') {
          continue
        }
        this.enumMulti.push({ id: arrEnumid[i], name: arrEnumval[i] })
      }
      this.enumMultishow = true
    },
    toggle (index) {
      this.$refs.checkboxes[index].toggle()
    },
    selectedEnumMulti () {
      let enumid = ''
      let enumval = ''
      this.clickmodel.valuehidden = ''
      this.clickmodel.value = ''
      for (let i = 0; i < this.selecteddata.length; i++) {
        enumid += this.selecteddata[i].id + ';'
        enumval += this.selecteddata[i].name + ';'
      }
      this.clickmodel.valuehidden = enumid
      this.clickmodel.value = enumval
      this.clickmodel.errormsg = ''
      this.enumMultishow = false
    },
    openRefer (datamodel) {
      this.actions = []
      this.clickmodel = datamodel
      let referVal = datamodel.sFormat.substring(6, datamodel.sFormat.length - 1)
      let arrRefer = referVal.split(',')
      for (let i = 0; i < arrRefer.length; i++) {
        this.actions.push({ id: arrRefer[i], name: arrRefer[i] })
      }
      this.enumshow = true
    },
    openBool (datamodel) {
      this.actions = []
      this.clickmodel = datamodel
      this.actions.push({ id: '1', name: '是' })
      this.actions.push({ id: '0', name: '否' })
      this.enumshow = true
    },
    boolUpdate (clickmodel) {
      //console.log('boolUpdate')
      let data = {}
      if (clickmodel.idObject === 'sorder' && clickmodel.sField === 'sfdj') {
        let sfdjval = clickmodel.value === '是' ? '带价格 ' : ''
        let fcval = ''
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            if (itemgroup[itemindex].sField === 'fc') {
              fcval = itemgroup[itemindex].value
            }
          }
          for (let itemindex in itemgroup) {
            if (itemgroup[itemindex].sField === 'snote') {
              itemgroup[itemindex].value = sfdjval + fcval
            }
          }
        }
      }
      if (clickmodel.idObject === 'sorder' && clickmodel.sField === 'fc') {
        let sfdjval = ''
        let fcval = clickmodel.value
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            if (itemgroup[itemindex].sField === 'sfdj') {
              sfdjval = itemgroup[itemindex].value === '是' ? '带价格 ' : ''
            }
          }
          for (let itemindex in itemgroup) {
            if (itemgroup[itemindex].sField === 'snote') {
              itemgroup[itemindex].value = sfdjval + fcval
            }
          }
        }
      }
    },
    openRef (datamodel, datafrom, indexName, index) {
      // alert(JSON.stringify(datamodel.jsfunc) + JSON.stringify(datamodel.idObject))
      // console.log('********^^^^^', datamodel)
      this.clickmodel = datamodel
      this.clickfrom = datafrom
      this.clickindex = index
      this.clickindexName = indexName
      this.conditionValHidden = ''
      let inview = datamodel.inview
      if (inview !== '') {
        let arrinview = inview.split(';')
        for (let inviewindex in arrinview) {
          let arrinviewdata = arrinview[inviewindex]
          let inviewdatas = arrinviewdata.split(':')
          if (inviewdatas[1] === 'eq') {
            if (datafrom === 'headdata') {
              for (let groupindex in this.jsondata.head) {
                for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
                  let item = this.jsondata.head[groupindex].groupvalue[itemindex]
                  if (item.sField === inviewdatas[2].split('_')[2]) {
                    this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                  }
                }
              }
            } else if (datafrom === 'bodydata') {
              for (let itemindex in this.jsondata.bodydata[index]) {
                let item = this.jsondata.bodydata[index][itemindex]
                if (item.sField === inviewdatas[0]) {
                  this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                }
              }
            }
          } else if (inviewdatas[1] === 'in') {
            this.conditionValHidden += inviewdatas[0] + ':in:;'
          }
        }
      }
      let refparams = ''
      // 每个逻辑需要单独写
      if (datamodel.idObject === 'sorders' && datamodel.jsfunc === 'getAccountForSorder()') {
        let agent = ''
        let consumer = ''
        let type = ''
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex]
            if (item.sField === 'agent') {
              agent = item.valuehidden
            } else if (item.sField === 'consumer') {
              consumer = item.valuehidden
            } else if (item.sField === 'type') {
              type = item.valuehidden
            }
          }
        }
        if (consumer === '') {
          Toast('请先选择客户')
          return
        }
        if (type === '297ef43353a100de0153a1fdcaa60611') {
          if (agent !== '') {
            Toast('自营模式不能输入代理商')
            return
          }
        } else {
          if (agent === '') {
            Toast('请先选择代理商')
            return
          }
        }
      }

      if (/^sorders\d?$/.test(datamodel.idObject) && datamodel.jsfunc === 'getAccountForSorderMkw()') {
        let consumer = ''
        let sheng = ''
        let date = ''
        let country = ''

        const fieldMap = {
          consumer: (value) => consumer = value,
          date: (value) => date = value,
          sheng: (value) => sheng = value,
          country: (value) => country = value
        }

        //console.log('this.jsondata.head', this.jsondata.head)
        this.jsondata.head.forEach(group => {
          group.groupvalue.forEach(item => {
            const handler = fieldMap[item.sField]
            if (handler) {
              handler(item.valuehidden || item.value)
            }
          })
        })

        sheng = sheng.length ? sheng : ''

        if (!date) {
          Toast('单据日期不能为空')
          return
        }

        if (consumer) {
          refparams = `${consumer};${date};${sheng};${country}`
        } else {
          Toast('请先输入客户')
          return
        }
      }

      if ((datamodel.idObject === 'arvouch' || datamodel.idObject === 'arvouch1') && datamodel.jsfunc === 'chenxin_arvouch_u8zdr()') { // 辰欣项目代码
        let json = {
          'arvouch_u8zth': document.getElementById('u8zth').value,
          'arvouch_iddep': document.getElementById('iddep').value
        }
        //console.log(JSON.stringify(json))
        refparams = base64encode(JSON.stringify(json))
        // alert(refparams)
      }
      // U8制单人
      if (datamodel.idObject === 'sorder' && datamodel.jsfunc === 'chenxinSorderAgent()') { // 辰欣项目代码
        let agent = ''
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex]
            if (item.sField === 'agent') {
              agent = item.valuehidden
            }
          }
        }
        if (agent === '') {
          Toast('请先选择信用客户')
          return
        }
        refparams = agent
      }

      // 费用报销单表体费用类别
      if (datamodel.idObject === 'fee' && datamodel.jsfunc === 'getFeebillFyzl()') { // 39项目代码
        let json = {
          'feebill_fyzl': document.getElementById('fyzl').value
        }
        //console.log(JSON.stringify(json))
        refparams = base64encode(JSON.stringify(json))
      }

      // 百奥 商业客户参照
      if (datamodel.idObject === 'sorder' && datamodel.jsfunc === '()') {
        let date = ''
        let tgxydh = ''
        let psxydh = ''

        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex]
            //console.log('itemindex:' + item.sField)
            if (item.sField === 'date') {
              date = item.value
            }
          }
        }

        for (let groupindex in this.jsondata.bodydata) {
          for (let itemindex in this.jsondata.bodydata[groupindex]) {
            let item = this.jsondata.bodydata[groupindex][itemindex]
            if (item.sField === 'tgxydh') {
              tgxydh = item.valuehidden
            } else if (item.sField === 'psxydh') {
              psxydh = item.valuehidden
            }
          }
        }

        if (!date) {
          Toast('单据日期不能为空!')
          return
        }
        if (tgxydh) {
          refparams = date + ';tgxy;' + tgxydh
        }
        if (psxydh) {
          refparams = date + ';psxy;' + psxydh
        }

        //console.log('xxxxxxx:' + 'refparams: ' + refparams)
      }

      this.$router.push({
        name: 'ref',
        params: {
          bo: datamodel.idJoinObject,
          tobo: datamodel.idObject,
          field: datamodel.sField,
          fieldref: datamodel.sFieldRef,
          viewid: datamodel.sRefKey,
          conditionVal: '',
          conditionValHidden: this.conditionValHidden,
          classname: datamodel.classname,
          params: refparams
        }
      })
    },
    openRefMulti (datamodel, datafrom, indexName, index) {
     
      this.clickmodel = datamodel
      this.clickfrom = datafrom
      this.clickindex = index
      this.clickindexName = indexName
      this.conditionValHidden = ''
      let inview = datamodel.inview
      if (inview !== '') {
        let arrinview = inview.split(';')
        for (let inviewindex in arrinview) {
          let arrinviewdata = arrinview[inviewindex]
          let inviewdatas = arrinviewdata.split(':')
          if (inviewdatas[1] === 'eq') {
            if (datafrom === 'headdata') {
              for (let groupindex in this.jsondata.head) {
                for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
                  let item = this.jsondata.head[groupindex].groupvalue[itemindex]
                  if (item.sField === inviewdatas[2].split('_')[2]) {
                    this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                  }
                }
              }
            } else if (datafrom === 'bodydata') {
              for (let itemindex in this.jsondata.bodydata[index]) {
                let item = this.jsondata.bodydata[index][itemindex]
                if (item.sField === inviewdatas[0]) {
                  this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                }
              }
            }
          } else if (inviewdatas[1] === 'in') {
            this.conditionValHidden += inviewdatas[0] + ':in:;'
          }
        }
      }
      // 每个逻辑需要单独写
      if (datamodel.idObject === 'sorders' && datamodel.jsfunc === 'getAccountForSorder()') {
        let agent = ''
        let consumer = ''
        let type = ''
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex]
            if (item.sField === 'agent') {
              agent = item.valuehidden
            } else if (item.sField === 'consumer') {
              consumer = item.valuehidden
            } else if (item.sField === 'type') {
              type = item.valuehidden
            }
          }
        }
        if (consumer === '') {
          Toast('请先选择客户')
          return
        }
        if (type === '297ef43353a100de0153a1fdcaa60611') {
          if (agent !== '') {
            Toast('自营模式不能输入代理商')
            return
          }
        } else {
          if (agent === '') {
            Toast('请先选择代理商')
            return
          }
        }
      }
      this.$router.push({
        name: 'refmulti',
        params: {
          bo: datamodel.idJoinObject,
          tobo: datamodel.idObject,
          field: datamodel.sField,
          fieldref: datamodel.sFieldRef,
          viewid: datamodel.sRefKey,
          conditionVal: '',
          conditionValHidden: this.conditionValHidden,
          classname: datamodel.classname
        }
      })
    },
    clearRefValue (datamodel) {
      if (!datamodel.value) {
        return // 如果输入框的值为空，不做任何操作
      }
      datamodel.value = '' // 清空输入框的值
      if (datamodel.value === '') {
        datamodel.valuehidden = ''
      }
    },
    onImageHead (file, detail) {
      // 上传
      let deviceFile = []
      let formData = new FormData()
      if (Array.isArray(file)) {
        deviceFile = file
      } else {
        deviceFile.push(file)
      }
      for (let i = 0; i < deviceFile.length; i++) {
        formData.append('image' + i, deviceFile[i].file, deviceFile[i].name)
      }
      this.axios({
        method: 'post',
        url: '/api/upload.imagedo',
        data: formData,
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then(res => {
        if (res.data.flag === '0') {
          let fileInfo = res.data.fileInfo
          for (let headIdx = 0; headIdx < this.jsondata.head.length; headIdx++) {
            let headData = this.jsondata.head[headIdx].groupvalue
            for (let i = 0; i < headData.length; i++) {
              if (headData[i].sField === detail.name) {
                headData[i].valuehidden = fileInfo[0].newname
                headData[i].value = fileInfo[0].oldname
              }
            }
          }
          Toast('上传成功')
        } else {
          Toast('上传失败')
        }
      }).catch(res => {
        //console.log(res)
      })
      // let headdata = this.jsondata.head[0].groupvalue
      //console.log(file)
      //console.log(detail)
    },
    delImageHead (file, detail) {
      for (let headIdx = 0; headIdx < this.jsondata.head.length; headIdx++) {
        let headData = this.jsondata.head[headIdx].groupvalue
        for (let i = 0; i < headData.length; i++) {
          if (headData[i].sField === detail.name) {
            let imgVal = headData[i].valuehidden.split(';')
            let imgTitle = headData[i].value.split(';')
            for (let j = 0; j < imgTitle.length; j++) {
              if (imgTitle[j] === file.file.name) {
                imgVal[j] = ''
                imgTitle[j] = ''
              }
            }
            let imgVal1 = ''
            let imgTitle1 = ''
            for (let j = 0; j < imgTitle.length; j++) {
              if (imgTitle[j] !== '') {
                imgVal1 += imgVal[j] + ';'
                imgTitle1 += imgTitle[j] + ';'
              }
            }
            headData[i].valuehidden = imgVal1
            headData[i].value = imgTitle1
          }
        }
      }
      Toast('删除成功')
    },
    onFileHead (file, detail) {
      // 上传
      let deviceFile = []
      let formData = new FormData()
      if (Array.isArray(file)) {
        deviceFile = file
      } else {
        deviceFile.push(file)
      }
      for (let i = 0; i < deviceFile.length; i++) {
        formData.append('file' + i, deviceFile[i].file, deviceFile[i].name)
      }
      this.axios({
        method: 'post',
        url: '/api/upload.filedo',
        data: formData,
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then(res => {
        if (res.data.flag === '0') {
          let fileInfo = res.data.fileInfo
          for (let headIdx = 0; headIdx < this.jsondata.head.length; headIdx++) {
            let headData = this.jsondata.head[headIdx].groupvalue
            for (let i = 0; i < headData.length; i++) {
              if (headData[i].sField === detail.name) {
                headData[i].valuehidden = fileInfo[0].newname
                headData[i].value = fileInfo[0].oldname
              }
            }
          }
          Toast('上传成功')
        } else {
          Toast('上传失败')
        }
      }).catch(res => {
        //console.log(res)
      })
      // let headdata = this.jsondata.head[0].groupvalue
      //console.log(file)
      //console.log(detail)
    },
    delFileHead (file, detail) {
      for (let headIdx = 0; headIdx < this.jsondata.head.length; headIdx++) {
        let headData = this.jsondata.head[headIdx].groupvalue
        for (let i = 0; i < headData.length; i++) {
          if (headData[i].sField === detail.name) {
            let fileVal = headData[i].valuehidden.split(';')
            let fileTitle = headData[i].value.split(';')
            for (let j = 0; j < fileTitle.length; j++) {
              if (fileTitle[j] === file.file.name) {
                fileVal[j] = ''
                fileTitle[j] = ''
              }
            }
            let fileVal1 = ''
            let fileTitle1 = ''
            for (let j = 0; j < fileTitle.length; j++) {
              if (fileTitle[j] !== '') {
                fileVal1 += fileVal[j] + ';'
                fileTitle1 += fileTitle[j] + ';'
              }
            }
            headData[i].valuehidden = fileVal1
            headData[i].value = fileTitle1
          }
        }
      }
      Toast('删除成功')
    },
    removeMyKeyFromLocalStorage (key) {
      //console.log('removing key ' + key)
      localStorage.removeItem(key)
    }

  }
  // activated() {

  //   if (this.$store.state.ref_show !== '') {

  //     this.clickmodel.value = this.$store.state.ref_show
  //     this.clickmodel.value = this.$store.state.ref_show
  //     this.clickmodel.valuehidden = this.$store.state.ref_id
  //     this.clickmodel.errormsg = ''

  //     if (this.$store.state.ref_map) {
  //       if (this.clickfrom === 'headdata') {
  //         this.handleRefMap("headdata", this.jsondata.head, this.$store.state.ref_map)

  //       } else if (this.clickfrom === 'bodydata') {
  //         //console.log("*******",this.clickindexName,this.clickindex)

  //         this.handleRefMap("bodydata", this.jsondata.bodydata[this.clickindexName][this.clickindex], this.$store.state.ref_map)
  //       }

  //     }

  //     // return

  //     this.clickmodel = {}
  //     this.clickindex = 0
  //     this.clickindexName = ''
  //     this.clickfrom = ''
  //     this.$store.commit('clearRef')

  //   }

  //   return

  //   if (this.$store.state.ref_show !== '') {
  //     this.clickmodel.value = this.$store.state.ref_show
  //     this.clickmodel.valuehidden = this.$store.state.ref_id
  //     this.clickmodel.errormsg = ''
  //     let fieldmap = this.clickmodel.fieldmap
  //     if (fieldmap !== undefined && fieldmap.length > 0) {
  //       let arrFieldmap = fieldmap.split(';')
  //       let item = this.$store.state.ref_item
  //       if (this.clickfrom === 'headdata') {
  //         for (let groupindex = 0; groupindex < this.jsondata.head.length; groupindex++) {
  //           let itemgroup = this.jsondata.head[groupindex]
  //           for (let itemindex = 0; itemindex < itemgroup.groupvalue.length; itemindex++) {
  //             let itemdata = itemgroup.groupvalue[itemindex]
  //             for (let fieldmapindex in arrFieldmap) {
  //               let arrFieldMapKey = arrFieldmap[fieldmapindex].split(':')
  //               if (this.$store.state.ref_field === arrFieldMapKey[1]) {
  //                 continue
  //               }
  //               if (arrFieldMapKey.length === 3 && arrFieldMapKey[2] !== '' && this.$store.state.ref_field !== arrFieldMapKey[2]) {
  //                 continue
  //               }
  //               if (itemdata.sField === arrFieldMapKey[1]) {
  //                 if (itemdata.sUIType === 'ref') {
  //                   itemdata.value = item[arrFieldMapKey[0] + '_ref']
  //                   itemdata.valuehidden = item[arrFieldMapKey[0]]
  //                 } else if (itemdata.sUIType === 'enum') {
  //                   itemdata.value = item[arrFieldMapKey[0] + '_enum']
  //                   itemdata.valuehidden = item[arrFieldMapKey[0]]
  //                 } else {
  //                   itemdata.value = item[arrFieldMapKey[0]]
  //                 }
  //               }
  //             }
  //           }
  //         }
  //       } else if (this.clickfrom === 'bodydata') {
  //         for (let itemindex in this.jsondata.bodydata[this.clickindex]) {
  //           let itemdata = this.jsondata.bodydata[this.clickindex][itemindex]
  //           for (let fieldmapindex in arrFieldmap) {
  //             let arrFieldMapKey = arrFieldmap[fieldmapindex].split(':')
  //             if (this.$store.state.ref_field === arrFieldMapKey[1]) {
  //               continue
  //             }
  //             if (arrFieldMapKey.length === 3 && arrFieldMapKey[2] !== '' && this.$store.state.ref_field !== arrFieldMapKey[2]) {
  //               continue
  //             }
  //             if (itemdata.sField === arrFieldMapKey[1]) {
  //               if (itemdata.sUIType === 'ref') {
  //                 itemdata.value = item[arrFieldMapKey[0] + '_ref']
  //                 itemdata.valuehidden = item[arrFieldMapKey[0]]
  //               } else if (itemdata.sUIType === 'enum') {
  //                 itemdata.value = item[arrFieldMapKey[0] + '_enum']
  //                 itemdata.valuehidden = item[arrFieldMapKey[0]]
  //               } else {
  //                 itemdata.value = item[arrFieldMapKey[0]]
  //               }
  //             }
  //           }
  //         }
  //       }
  //     }
  //     this.clickmodel = {}
  //     this.clickindex = 0
  //     this.clickfrom = ''
  //     this.$store.commit('clearRef')
  //   }
  // }

}
</script>

<style>
.fieldgroup_title {
  text-align: left;
  margin: 0px;
  padding: 25px 15px 15px;
  color: rgba(69, 90, 100, 0.6);
  font-weight: normal;
  font-size: 14px;
}
</style>
